package com.xy.configs;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Slf4j
@PropertySource("classpath:application.yml")
@Configuration
public class RedisConfig {

		@Value("${spring.redis.host}")
		private String host;

		@Value("${spring.redis.port}")
		private int port;

		@Value("${spring.redis.timeout}")
		private int timeout;

		@Value("${spring.redis.jedis.pool.maxIdle}")
		private int maxIdle;

		@Value("${spring.redis.jedis.pool.max-wait}")
		private int maxWaitMillis;

		@Bean
		public JedisPool jedisPoolFactory() {
				JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
				jedisPoolConfig.setMaxIdle(maxIdle);
				jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
				// 连接耗尽时是否阻塞, false报异常,true阻塞直到超时, 默认true
				jedisPoolConfig.setBlockWhenExhausted(true);
				JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout);
				log.info("JedisPool注入成功..."+jedisPool);
				return jedisPool;
		}

}